package swingFiles;

import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import mainFiles.GmsDB;
import mainFiles.Class;

/**
 * A panel that allows a user to delete a class from the database
 * @author Emily Linniman
 * @version December 1, 2014
 */
@SuppressWarnings("serial")
public class cancelClassPanel extends JPanel {
	
	/** The field for the id input. */
	private JTextField idField;
	
	/** The database object which interacts with the gym management system database. */
	private GmsDB db;
	
	/** The frame the panel is located on. */
	private JFrame my_frame;

	/**
	 * Create the panel.
	 */
	public cancelClassPanel(JFrame the_frame, final JPanel previous_panel) {
		my_frame = the_frame;

		db = new GmsDB();
		setBackground(Color.ORANGE);
		setLayout(null);
		
		JLabel headerLabel = new JLabel("Cancel Classes");
		headerLabel.setFont(new Font("Arial", Font.PLAIN, 18));
		headerLabel.setBounds(160, 6, 146, 16);
		add(headerLabel);
		
		JLabel instructionsLabel = new JLabel("Enter the ID number of the class you'd like to cancel:");
		instructionsLabel.setFont(new Font("Arial", Font.PLAIN, 15));
		instructionsLabel.setBounds(55, 61, 359, 50);
		add(instructionsLabel);
		
		idField = new JTextField();
		idField.setHorizontalAlignment(SwingConstants.CENTER);
		idField.setBounds(157, 138, 134, 28);
		add(idField);
		idField.setColumns(9);
		
		
		JButton cancelButton = new JButton("Cancel Class");
		cancelButton.setFont(new Font("Arial", Font.PLAIN, 15));
		cancelButton.setBounds(160, 198, 131, 29);
		cancelButton.addActionListener(new ActionListener() {
			
			//Checks that id is valid, if it is, deletes class
			public void actionPerformed(ActionEvent arg0) {
				String id = idField.getText();
				
				if(id != null && id.length() == 9) {
					List<Class> classes = new ArrayList<Class>();
					boolean found = false;
					try {
						classes = db.getClasses();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					
					for (Class currClass : classes) {
						if (currClass.getID().equals(id)) {
							found = true;
						}
					}
					
					if (found) {
						db.deleteClass(id);
						JOptionPane.showMessageDialog(my_frame, "Class Successfully Cancelled!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(my_frame, "The class does not exist!", "Error!", JOptionPane.PLAIN_MESSAGE);
					}
				}
			}
		});
		add(cancelButton);
		
		JButton backButton = new JButton("Back");
		backButton.setFont(new Font("Arial", Font.PLAIN, 15));
		backButton.setBounds(160, 245, 131, 29);
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				my_frame.setContentPane(previous_panel);
				my_frame.validate();
				my_frame.setVisible(true);
			}
		});
		add(backButton);
		

	}
}
